<template>
  <div class="app-container">
    <div class="container">
      <!--工具栏-->
      <div>
        <el-form ref="query" :inline="true" :model="query" size="small" class="demo-form-inline">
          <el-form-item label="攻略名称：" >
            <el-input
              v-model="query.title"
              clearable
              placeholder="输入攻略名称"
              style="width: 180px;"
              class="filter-item"
              @keyup.enter.native="toQuery"/>
          </el-form-item>
          <el-form-item>
            <el-button size="mini" type="primary" icon="el-icon-search" @click="toQuery">搜索</el-button>
            <el-button size="mini" icon="el-icon-refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <div class="operate-container">
          <el-button
            class="filter-item"
            size="mini"
            type="primary"
            icon="el-icon-plus"
            @click="add">添加攻略
          </el-button>
        </div>
      </div>
      <!--表格渲染-->
      <el-table v-loading="loading" :data="data" size="small" style="width: 100%;">
        <el-table-column
          label="序号"
          width="70px"
          align="center">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column ref="table" :show-overflow-tooltip="true" prop="title" label="攻略标题" align="center"/>
        <el-table-column label="创建时间" prop="create_date" align="center"/>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <!--发布评论触发事件-->
            <!--编辑按钮-->
            <el-button slot="reference" type="primary" icon="el-icon-upload" size="mini" @click="enter(scope.row)">编辑</el-button>
            <!--删除触发事件-->
            <el-popover
              :ref="scope.row.strategy_id"
              placement="top"
              width="180">
              <p>确定删除这个攻略吗？</p>
              <div style="text-align: right; margin: 0">
                <el-button size="mini" type="text" @click="$refs[scope.row.strategy_id].doClose()">取消</el-button>
                <el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row)">确定</el-button>
              </div>
              <!--删除按钮-->
              <el-button slot="reference" type="danger" icon="el-icon-delete" size="mini">删除</el-button>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <el-pagination
        :total="total"
        :current-page="page + 1"
        background
        style="margin-top: 8px;"
        layout="total, prev, pager, next, sizes"
        @size-change="sizeChange"
        @current-change="pageChange"/>
    </div>
  </div>
</template>

<script>
import initData from '@/mixins/initData'
import { deleteStrategy } from '@/api/mall/strategy'

export default {
  mixins: [initData],
  data() {
    return {
      query: {
        title: ''
      },
      delLoading: false,
      releaseLoading: false
    }
  },
  created() {
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    beforeInit() {
      this.url = 'api/strategy-list'
      const sort = '0'
      this.params = { page: this.page, size: this.size, sort: sort }
      const query = this.query
      const name = query.title
      if (name && name !== '' && name !== null) {
        this.params['title'] = name
      }
      return true
    },
    enter(obj) {
      this.$router.push({ path: '/strategy/addStrategy', query: {
        type: 2, 'strategy_id': obj.strategy_id
      }})
    },
    subDelete(info) {
      // 删除
      console.log(info)
      this.delLoading = true
      const data = { 'strategy_id': info.strategy_id }
      deleteStrategy(data).then(() => {
        this.delLoading = false
        this.$refs[info.strategy_id].doClose()
        this.$notify({
          title: '删除成功',
          type: 'success',
          duration: 2500
        })
        this.dleChangePage()
        this.init()
      }).catch(() => {
        this.delLoading = false
        this.$refs[info.strategy_id].doClose()
      })
    },
    add() {
      this.$router.push({ path: '/strategy/addStrategy', query: {
        type: 1
      }})
    },
    resetQuery() {
      // 重置，重置表单，刷新页面
      this.$refs['query'].resetFields()
      this.query = {
        title: ''
      }
      this.init()
    }
  }
}
</script>

<style scoped>
  .operate-container{
    float: left;
  }
</style>
